package com.java.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import com.java.dao.OrderDao;
import com.java.dao.TimeslotDao;
import com.java.entity.Doctor;
import com.java.entity.Order;
import com.java.entity.Timeslot;
import com.java.ext.MyButton;
import com.java.utils.MyWindowListener;

/**
* @ClassName: TimeslotWindow
* @Description: 坐诊设置与选择界面
* @author ChenJiaLu
* @date 2023年12月29日 下午2:55:09
*
 */
public class TimeslotWindow extends BaseJFrame {
	private static final long serialVersionUID = 1L;
	
	/**
	 * 父级窗口
	 */
	JFrame pJFrame;
	
	private Doctor selectObj = null;

	// 构造函数  
    public TimeslotWindow(JFrame bJFrame, Doctor doctor) {
    	pJFrame = bJFrame;
    	selectObj = doctor;
        // 设置窗口属性  
        setTitle(null == selectObj ?"坐诊时间设置": "选择预约日期");
		setSize(350, 270);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);  
        addWindowListener(new CloseButtonListener()); 
        setLocationRelativeTo(null);
  
        // 创建用于布局的 JPanel  
        JPanel contentPane = new JPanel();  
        contentPane.setLayout(new GridLayout(7, 2)); // 使用网格布局管理器  
  
        String[] options = {"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"};  
        
        // 创建编号输入框  
        
        final JCheckBox[] checkBoxes = new JCheckBox[options.length];  
        final JRadioButton[] radioBoxes = new JRadioButton[options.length];  
		Timeslot ts = new TimeslotDao().getDoctorTimeslot(selectObj.getId());
        if(session.isAdmin()) {
        	for (int i = 0; i < options.length; i++) {  
                checkBoxes[i] = new JCheckBox(options[i]);  
            }   
            for (JCheckBox checkBox : checkBoxes) {
            	if(null != ts) {
            		if("星期一".equals(checkBox.getText()) && ts.getDay1() == 1) {
                		checkBox.setSelected(true);
                    }
                    if("星期二".equals(checkBox.getText()) && ts.getDay2() == 1) {
                    	checkBox.setSelected(true);
                    }
                    if("星期三".equals(checkBox.getText()) && ts.getDay3() == 1) {
                    	checkBox.setSelected(true);
                    }
                    if("星期四".equals(checkBox.getText()) && ts.getDay4() == 1) {
                    	checkBox.setSelected(true);
                    }
                    if("星期五".equals(checkBox.getText()) && ts.getDay5() == 1) {
                    	checkBox.setSelected(true);
                    }
                    if("星期六".equals(checkBox.getText()) && ts.getDay6() == 1) {
                    	checkBox.setSelected(true);
                    }
                    if("星期日".equals(checkBox.getText()) && ts.getDay7() == 1) {
                    	checkBox.setSelected(true);
                    }
            	}
            	contentPane.add(checkBox);
            }
        }
        if(session.isPatient()) {
        	ButtonGroup buttonGroup = new ButtonGroup();
        	for (int i = 0; i < options.length; i++) {  
        		radioBoxes[i] = new JRadioButton(options[i]);
        		buttonGroup.add(radioBoxes[i]);
            }   
            for (JRadioButton checkBox : radioBoxes) {
            	checkBox.setEnabled(false);
            	if(null != ts) {
            		if("星期一".equals(checkBox.getText()) && ts.getDay1() == 1) {
                		checkBox.setEnabled(true);
                    }
                    if("星期二".equals(checkBox.getText()) && ts.getDay2() == 1) {
                    	checkBox.setEnabled(true);
                    }
                    if("星期三".equals(checkBox.getText()) && ts.getDay3() == 1) {
                    	checkBox.setEnabled(true);
                    }
                    if("星期四".equals(checkBox.getText()) && ts.getDay4() == 1) {
                    	checkBox.setEnabled(true);
                    }
                    if("星期五".equals(checkBox.getText()) && ts.getDay5() == 1) {
                    	checkBox.setEnabled(true);
                    }
                    if("星期六".equals(checkBox.getText()) && ts.getDay6() == 1) {
                    	checkBox.setEnabled(true);
                    }
                    if("星期日".equals(checkBox.getText()) && ts.getDay7() == 1) {
                    	checkBox.setEnabled(true);
                    }
            	}
            	contentPane.add(checkBox);  
            }
        }
        
        // 创建重置按钮和提交按钮的面板  
        JPanel buttonPanel = new JPanel();  
        buttonPanel.setLayout(new FlowLayout()); // 使用流式布局，使按钮水平排列
        MyButton returnBtn = new MyButton("返回").defaultt();
        buttonPanel.add(returnBtn);  
        MyButton saveBtn = new MyButton("提交").primary();
        buttonPanel.add(saveBtn);  
  
        // 将按钮面板添加到主窗口的底部  
        getContentPane().add(contentPane, BorderLayout.CENTER);  
        getContentPane().add(buttonPanel, BorderLayout.SOUTH); // 添加到底部 
		
        returnBtn.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            	setVisible(false);
            	pJFrame.setEnabled(true);
            }  
        }); 
		
        saveBtn.addActionListener(new ActionListener() {  
            public void actionPerformed(ActionEvent e) {  
            	if(session.isAdmin()) {
            		Timeslot ts = new TimeslotDao().getDoctorTimeslot(selectObj.getId());
            		if(null != ts) {
            			new TimeslotDao().delTimeslot(ts.getId());
            		}
            		ts = new Timeslot();
            		for (JCheckBox checkBox : checkBoxes) {
                        if (checkBox.isSelected()) {  
                            if("星期一".equals(checkBox.getText())) {
                            	ts.setDay1(1);
                            }
                            if("星期二".equals(checkBox.getText())) {
                            	ts.setDay2(1);
                            }
                            if("星期三".equals(checkBox.getText())) {
                            	ts.setDay3(1);
                            }
                            if("星期四".equals(checkBox.getText())) {
                            	ts.setDay4(1);
                            }
                            if("星期五".equals(checkBox.getText())) {
                            	ts.setDay5(1);
                            }
                            if("星期六".equals(checkBox.getText())) {
                            	ts.setDay6(1);
                            }
                            if("星期日".equals(checkBox.getText())) {
                            	ts.setDay7(1);
                            }
                        }  
                    }
            		ts.setDoctorid(selectObj.getId());
            		new TimeslotDao().saveTimeslot(ts);
    				ok("设置成功");
            	}
            	if(session.isPatient()) {
            		String selectText = null;
            		for (JRadioButton radioBox : radioBoxes) {  
                        if (radioBox.isSelected()) {
                            selectText = radioBox.getText();
                        }  
                    }
            		if(null == selectText) {
            			warning("请选择就诊日期");
            			return ;
            		}
            		OrderDao dDao = new OrderDao();
    				Order order = new Order();
    				order.setDoctorid(selectObj.getId());
    				order.setPatientid(session.getUserId());
    				order.setTimeslot(selectText);
    				dDao.saveOrder(order);
    				ok("预约成功");
            	}
            	setVisible(false);
            	pJFrame.setEnabled(true);
            }  
        }); 
        
		setVisible(true);
		
    } 
    
    class CloseButtonListener extends MyWindowListener {
    	
    	@Override
    	public void windowClosing(WindowEvent e) {  
        	
        }
    }
      
}